package com.block.common.util;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Matcher;
import org.web3j.abi.datatypes.Type;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 
 * @date 2022/3/15 6:40 PM
 */
public class FutureUtils {
    /**
     * Future全部执行完毕
     * @param futureList
     * @return
     * @throws Exception
     */
    public static boolean isAllDone(List<Future> futureList) throws Exception {
//        List<Integer> list = new ArrayList<>();
//        int x=0;
        while (futureList.size() > 0) {
            Iterator<Future> iterable = futureList.iterator();
            while (iterable.hasNext()) {
                Future<Boolean> future = iterable.next();
                if (future.isDone() && !future.isCancelled()) {
//                    Object result = future.get();
//                    list.add(x++);
                    //完成的任务移除
                    iterable.remove();
                } else {
                    Thread.sleep(1);
                }
            }
        }
        return futureList.size()==0;
    }
    public static boolean isAllDoneTypes(List<Future<List<Type>>> futureList) throws Exception {
//        List<Integer> list = new ArrayList<>();
//        int x=0;
        while (futureList.size() > 0) {
            Iterator<Future<List<Type>>> iterable = futureList.iterator();
            while (iterable.hasNext()) {
                Future<List<Type>> future = iterable.next();
                if (future.isDone() && !future.isCancelled()) {
//                    Object result = future.get();
//                    list.add(x++);
                    //完成的任务移除
                    iterable.remove();
                } else {
                    Thread.sleep(1);
                }
            }
        }
        return futureList.size()==0;
    }
}
